package psk.lightshow;

import java.util.Timer;
import java.util.TimerTask;

import psk.lightshow.tool.DataUtil;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Show extends Activity {
	private Activity nowActivity = Show.this;
	private TextView tvShowText,tvShowDot;
	private Button btnTips,btnHoldHide,btnFullScreen;
	private RelativeLayout rlShowDot;
	private Timer tFlash,tInterval;
	private Bundle bl;
	private String content = "",type = "";
	private int width,r=0,g=0,b=0,flash=0,interval=0,clickCount=0;
	private int i=0,showModel = 1;
	
	Handler handler = new Handler(){
		public void handleMessage(Message msg){
			switch(msg.what){
			case DataUtil.GO_TO_SHOW:
				btnTips.setVisibility(View.GONE);
				type = bl.getString("type");
				if(type.equals("text")){
					i = 0;
					r = bl.getInt("r");
					g = bl.getInt("g");
					b = bl.getInt("b");
					flash     = bl.getInt("flash");
					content   = bl.getString("content");
					interval  = bl.getInt("interval");
					showModel = bl.getInt("model");
					tvShowText.setVisibility(View.VISIBLE);
					tvShowText.setTextSize((float) (width/1.8));
					tvShowText.setTextColor(Color.rgb(r, g, b));
					showModelTextTimer();
				}else if(type.equals("dot")){
					r = bl.getInt("r");
					g = bl.getInt("g");
					b = bl.getInt("b");
					int size = bl.getInt("size");
					rlShowDot.setVisibility(View.VISIBLE);
					RelativeLayout rlDot = (RelativeLayout)findViewById(R.id.rlDot);
					RelativeLayout.LayoutParams loParams = (RelativeLayout.LayoutParams)rlDot.getLayoutParams();
					loParams.width = size;
					loParams.height = size;
					rlDot.setLayoutParams(loParams);
					tvShowDot.setBackgroundColor(Color.rgb(r, g, b));
				}else if(type.equals("full")){
					r = bl.getInt("r");
					g = bl.getInt("g");
					b = bl.getInt("b");
					btnFullScreen.setVisibility(View.VISIBLE);
					btnFullScreen.setBackgroundColor(Color.rgb(r, g, b));
				}
				break;
			case DataUtil.TEXT_SHOW:
				if(i>=content.length() && showModel == 0) i = 0;
				tvShowText.setText(content.substring(i, i+1));
				i++;
				break;
			case DataUtil.TEXT_HIDE:
				tvShowText.setText("");
				if(i>=content.length() && showModel ==1){
					tFlash.cancel();
					tInterval.cancel();
					btnTips.setVisibility(View.VISIBLE);
					tvShowText.setVisibility(View.INVISIBLE);
				}
				break;
			}
		}
	};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	            WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.show);
		initView();
	}
	/**
	 * Initial the content view and set controls------------------------------------------------
	 */
	private void initView(){
		//Get the parameter from last activity---------------------------------------------------
		Intent it = this.getIntent();
		bl = it.getExtras();
		//Initial the control-------------------------------------------------------------------
		tvShowDot  = (TextView)findViewById(R.id.tvShowDot);
		tvShowText = (TextView)findViewById(R.id.tvShowText);
		rlShowDot  = (RelativeLayout)findViewById(R.id.rlShowDot);
		//Get the display screen pixel------------------------------------------------------------
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		width = dm.widthPixels;
		//Set the button of tips------------------------------------------------------------------
		btnTips = (Button)findViewById(R.id.btnTips);
		btnTips.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				new Thread(new Runnable(){
					public void run(){
						try {
							Thread.sleep(1000);
							handler.sendEmptyMessage(DataUtil.GO_TO_SHOW);
						} catch (InterruptedException e) {
						}
					}
				}).start();
			}
		});
		//Set the double click to quit-----------------------------------------------------
		btnHoldHide = (Button)findViewById(R.id.btnHoldHide);
		btnHoldHide.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				clickCount++;
				if(clickCount == 2){
					nowActivity.finish();
				}else{
					checkDouble();
				}
			}
		});
		btnHoldHide.setOnTouchListener(new OnTouchListener(){
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_UP && type.equals("dot")){  
                    tvShowDot.setBackgroundColor(Color.rgb(r, g, b)); 
                }   
                if(event.getAction() == MotionEvent.ACTION_DOWN && type.equals("dot")){  
                    tvShowDot.setBackgroundColor(Color.rgb(0, 0, 0));  
                } 
				return false;
			}
		});
		btnFullScreen = (Button)findViewById(R.id.btnFullScreen);
		btnFullScreen.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				clickCount++;
				if(clickCount == 2){
					nowActivity.finish();
				}else{
					checkDouble();
				}
			}
		});
		btnFullScreen.setOnTouchListener(new OnTouchListener(){
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_UP && type.equals("full")){  
                    btnFullScreen.setBackgroundColor(Color.rgb(r, g, b)); 
                }   
                if(event.getAction() == MotionEvent.ACTION_DOWN && type.equals("full")){  
                	btnFullScreen.setBackgroundColor(Color.rgb(0, 0, 0));  
                } 
				return false;
			}
		});
		
	}
	/**
	 * Use Timer to show text model:---------------------------------------------------
	 * flash is the time of show with millisecond
	 * interval is the time of hide with millisecond
	 */
	private void showModelTextTimer(){
		tFlash = new Timer(true);
		tInterval = new Timer(true);
		TimerTask taskFlash = new TimerTask(){
			public void run(){
				handler.sendEmptyMessage(DataUtil.TEXT_SHOW);
				
			}
		};
		TimerTask taskInterval = new TimerTask(){
			public void run(){
				handler.sendEmptyMessage(DataUtil.TEXT_HIDE);
			}
		};
		tFlash.schedule(taskFlash, 0, flash+interval);
		tInterval.schedule(taskInterval, flash, flash+interval);
	}
	/**
	 * Listener the double click to quit-------------------------------------------------
	 */
	private void checkDouble(){
		new Thread(new Runnable(){
			public void run(){
				try {
					Thread.sleep(500);
					clickCount = 0;
				} catch (InterruptedException e) {
					
				}
			}
		}).start();
	}

}
